package conson.tech.manage.common.service;

import conson.tech.manage.common.util.OssUploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;

/**
 * ClassName: OssFileService
 * Description:
 * Author: HXL
 * Date: 2020/4/23 6:37 下午
 * Version: 1.0
 */
@Slf4j
@Service("ossFileService")
public class OssFileService {

    /** oss域名 */
    @Value("${oss-server.endpoint}")
    private  String endpoint;
    /** ossAccess Key ID(用户名) */
    @Value("${oss-server.accessKeyId}")
    private  String accessKeyId;
    /** accessKeySecret(密码) */
    @Value("${oss-server.accessKeySecret}")
    private  String accessKeySecret;
    /** bucket名称 */
    @Value("${oss-server.bucketName}")
    private  String bucketName;
    @Value("${oss-server.http}")
    private String fileHttp;

    public  String fileUpload(MultipartFile file,String uploadDir){
        try {
            OssUploadUtils ossUploadUtils = new OssUploadUtils(bucketName, endpoint, accessKeyId, accessKeySecret);
            String suffix = getSuffix(file);
            return fileHttp+uploadDir+ossUploadUtils.upload(uploadDir, file.getInputStream(), suffix,file.getSize());
        } catch (Exception e) {
           log.error("OssFileService-->fileUpload fail. exception:",e);
        }
        return null;
    }
    public  String fileUploadSaveOldName(MultipartFile file,String uploadDir){
        try {
            OssUploadUtils ossUploadUtils = new OssUploadUtils(bucketName, endpoint, accessKeyId, accessKeySecret);
            //String suffix = getSuffix(file);
            return fileHttp+uploadDir+ossUploadUtils.upload(uploadDir, file.getOriginalFilename(),file.getInputStream(),file.getSize());
        } catch (Exception e) {
            log.error("OssFileService-->fileUpload fail. exception:",e);
        }
        return null;
    }
    public  String fileUpload(File file, String uploadDir){
        try {
            OssUploadUtils ossUploadUtils = new OssUploadUtils(bucketName, endpoint, accessKeyId, accessKeySecret);
            String suffix = getSuffix(file);
            return fileHttp+uploadDir+ossUploadUtils.upload(uploadDir,  new FileInputStream(file), suffix,file.length());
        } catch (Exception e) {
            log.error("OssFileService-->fileUpload fail. exception:",e);
        }
        return null;
    }
    /**
     * 获取文件后缀名
     *
     * @param file
     * @return
     */
    private  String getSuffix(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
        return suffix;
    }
    /**
     * 获取文件后缀名
     *
     * @param file
     * @return
     */
    private  String getSuffix(File file) {
        String fileName = file.getName();
        String suffix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
        return suffix;
    }
}
